/*
 * @Author: your name
 * @Date: 2021-11-12 21:33:25
 * @LastEditTime: 2021-11-19 17:01:56
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\serve\app\service\article.js
 */
/*
 * @Author: your name
 * @Date: 2021-11-10 08:34:30
 * @LastEditTime: 2021-11-11 09:09:44
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\service\login.js
 */

const Service = require("egg").Service;
const timeFormart = require("../utils/timeFormart");
class FileService extends Service {
    //获取文件数据
    async getFileList() {
            const { ctx, app } = this;
            const { pageSize, pageIndex, title, type } = ctx.request.body;
            let res = await app.mysql.select("files");
            let total = res.length;
            //名称
            if (title) {
                res = res.filter((item) => {
                    if (
                        item.file_name.indexOf(title) !== -1 ||
                        item.file_name.includes(title)
                    ) {
                        return item;
                    }
                });
                total = res.length;
            }
            //文件类型
            if (type) {
                res = res.filter((item) => {
                    if (
                        item.file_type.indexOf(type) !== -1 ||
                        item.file_type.includes(type)
                    ) {
                        return item;
                    }
                });
                total = res.length;
            }
            //分页
            res.sort((a, b) => {
                return new Date(b.data) - new Date(a.data);
            });
            res = res.slice((pageIndex - 1) * pageSize, pageSize * pageIndex);
            return { res, total };
        }
        //更改文章状态
    async delFileList() {
        const { ctx, app } = this;
        const { id } = ctx.request.body;
        //找到原数据
        const res = await app.mysql.delete("files", {
            id,
        });
        return res;
    }
}
module.exports = FileService;